/************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     tp_rtp.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-01
  * @brief     
 *************************************************************************/

#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "tp_rtp.h"
#include "function.h"
#include "function_drv.h"
#include "bsp.h"
#include "eflash.h"

#include "adc.h"

uint8_t Rtp_Cor = 0;

//#define flh_sAddr   0x6007F000   //(2M-4k)

#if (LT_TOUCH_FLAG == 1)

// Save RTP calibration parameters
void LT_TpSaveAdjdata(void)
{
	uint8_t reVal, buff[4096] = {0};

	gTpInfo.flag = 0x5a;
	buff[0] = gTpInfo.flag;

	memcpy(&buff[1], gTpxfac.s, 4);
	memcpy(&buff[5], gTpxoff.s, 4);
	memcpy(&buff[9], gTpyfac.s, 4);
	memcpy(&buff[13], gTpyoff.s, 4);

	volatile uint32_t ier;

	ier = EIC->IER;		//Disable All Interrupts
	EIC->IER = 0;

	EFlash_Init();
	EFLASH_Write(flh_sAddr,buff,4096);

	EIC->IER = ier;				//enable All Interrupts

	// dubug
//		LTPrintf("write  \r\n");
	//	printf("gTpxfac.s  %f  \r\n",gTpxfac.xfac);
	//	printf("gTpxfac.s  %f  \r\n",gTpxoff.xoff);
	//	printf("gTpxfac.s  %f  \r\n",gTpyfac.yfac);
	//	printf("gTpxfac.s  %f  \r\n",gTpyoff.yoff);
}

// Read RTP calibration parameters
uint8_t LT_TpGetAdjdata(void)
{
	uint8_t i, buff[4096] = {0};

	EFlash_Read(flh_sAddr,buff,17);

	if (buff[0] == 0x5a)
	{
		memcpy(gTpxfac.s, &buff[1], 4);
		memcpy(gTpxoff.s, &buff[5], 4);
		memcpy(gTpyfac.s, &buff[9], 4);
		memcpy(gTpyoff.s, &buff[13], 4);

		gTpInfo.xfac = gTpxfac.xfac;
		gTpInfo.xoff = gTpxoff.xoff;
		gTpInfo.yfac = gTpyfac.yfac;
		gTpInfo.yoff = gTpyoff.yoff;

		// dubug
		//		LTPrintf("read  \r\n");
		//		printf("gTpxfac.s  %f  \r\n",gTpInfo.xfac);
		//		printf("gTpxfac.s  %f  \r\n",gTpInfo.xoff);
		//		printf("gTpxfac.s  %f  \r\n",gTpInfo.yfac);
		//		printf("gTpxfac.s  %f  \r\n",gTpInfo.yoff);

		return 1;
	}
	else
		return 0;
}

void LT_TpDelay(void)
{
	uint8_t t = 1;
	while (t--)
	{
		asm("nop");
		asm("nop");
		asm("nop");
		asm("nop");
		asm("nop");
	}
}

// X+:AIN[4]
// X-:AIN[6]
// Y+:AIN[5]
// Y-:AIN[7]
void ADC_Initial(void)
{
/*
	ADC->CFGR2 |= ((10 - 1) << 8); // 10MHz/10=1MHz
	ADC->CFGR2 |= 0x20;			   // Start counting digit
	//	ADC->CFGR2 |= 2*72/6/12;   // Start counting digit

	ADC->CFGR1 &= ~(1 << 20); // Discontinuous conversion
	ADC->CFGR1 &= ~(7 << 24); // The sequence length is 1 unit
	ADC->CFGR1 &= ~(3 << 8);  // resolution ratio:12
	ADC->CFGR1 &= ~(1 << 10); // Align Right
	ADC->SMPR = 0x20;		  // Sampling time
*/

	//锟斤拷锟斤拷ADC锟斤拷锟斤拷
//	OPTION->ADCCDISR = 0x4000;
//	OPTION->ADCCDISR = 0x8000;
//	OPTION->ADCCDISR = 0xC000;

	u16 PreClk_DIVx = 11;

	ADC->U32_ADC_CFGR2.QPR = PreClk_DIVx;             // 预锟斤拷频时锟斤拷位
	ADC->U32_ADC_CFGR2.STCNT = 2*(168/(PreClk_DIVx+1)); // 锟斤拷锟斤拷锟斤拷锟斤拷位

	ADC->U32_ADC_CFGR1.CONT = 0;					//锟截憋拷锟斤拷锟斤拷转锟斤拷
	ADC->U32_ADC_CFGR1.SEQ_LEN = 0;					//转锟斤拷锟斤拷锟斤拷为1
	ADC->U32_ADC_CFGR1.RES = 0;						//12位
	ADC->U32_ADC_CFGR1.ALIGN = 0;					//锟揭讹拷锟斤拷

//	ADC->U32_ADC_CHSELR1.CCW0 = 1;					//选锟斤拷转锟斤拷通锟斤拷0锟斤拷转锟斤拷ADC_In1

	ADC->U32_ADC_SMPR.SMP = 6-2;						//锟斤拷锟斤拷时锟斤拷

#if	ADC_IER_TEST_CTRL
	ADC->U32_ADC_IER.EOSEQIE = 1;
	EIC->IER|=IRQ(0);
#endif

	ADC->U32_ADC_CR.ADEN = 1;						//使锟斤拷ADC
	while(ADC->U32_ISR.ADRDY==0);				//锟饺达拷ADC锟斤拷锟斤拷


/*
 // TEST
	OPTION->U16_ADCCDISR.ADCCDISTEST = 1;
	OPTION->U16_ADCCDISR.ADCCDISTEST = 2;
	OPTION->U16_ADCCDISR.ADCCDISTEST = 3;
	OPTION->U16_ADCCDISR.ADCCDIS &=~((1<<4)|(1<<5)|(1<<6)|(1<<7));
	uint16_t ain0_Value;
	while(1)
	{
//	OPTION->U16_ADCCDISR.ADCCDISTEST = 1;
//	OPTION->U16_ADCCDISR.ADCCDISTEST = 2;
//	OPTION->U16_ADCCDISR.ADCCDISTEST = 3;
//	OPTION->U16_ADCCDISR.ADCCDIS |= ((1<<4)|(1<<5)|(1<<6)|(1<<7));

	ain0_Value = Get_ADC_Val(ADC_CH4); // Y+ get ADC value
	LTPrintf("AIN0=%d  \r\n",ain0_Value);
	ain0_Value = Get_ADC_Val(ADC_CH5); // Y+ get ADC value
	LTPrintf("AIN0=%d  \r\n",ain0_Value);
	ain0_Value = Get_ADC_Val(ADC_CH6); // Y+ get ADC value
	LTPrintf("AIN0=%d  \r\n",ain0_Value);
	ain0_Value = Get_ADC_Val(ADC_CH7); // Y+ get ADC value
	LTPrintf("AIN0=%d  \r\n",ain0_Value);
	LTPrintf(" \r\n");

	//	OPTION->U16_ADCCDISR.ADCCDISTEST = 1;
	//	OPTION->U16_ADCCDISR.ADCCDISTEST = 2;
	//	OPTION->U16_ADCCDISR.ADCCDISTEST = 3;
	//	OPTION->U16_ADCCDISR.ADCCDIS &=~((1<<4)|(1<<5)|(1<<6)|(1<<7));

	DelayMS(1000);
	}*/

}
u16 rtp_Get_ADC_Val(ADC_CH ch)
{
	u16 Val = 0;
	ADC->U32_ADC_CHSELR1.CCW0 = ch;					//选锟斤拷转锟斤拷通锟斤拷
	ADC->U32_ADC_CR.ADSTART = 1;						//锟斤拷始转锟斤拷
	while(ADC->U32_ISR.EOSEQ==0);				//锟饺达拷转锟斤拷锟斤拷锟叫斤拷锟斤拷

	ADC->ADC_ISR |= ADC_FLAG_EOSEQ;					//锟斤拷锟斤拷锟斤拷锟斤拷锟街�
//	LTPrintf("ADC->ADC_ISR:0x%x\r\n",ADC->ADC_ISR);
//	LTPrintf("ADC->ADC_CR:0x%x\r\n",ADC->ADC_CR);
//	DelayMs(10);
	Val = ADC->ADC_FIFO;							//锟斤拷锟斤拷锟斤拷

	return Val;
}
/*uint16_t Get_ADC_Val(uint8_t ADC_Channel_x)
{
	uint32_t adcisr = 0, i;

	uint16_t Val = 0;

	ADC_Initial();

	ADC->CHSELR1 = ADC_Channel_x;
	ADC_Cmd(ADC_EN);

	for (i = 0; i < 4; i++)
	{
		ADC_StartConv();
		DelayMS(1);
		while (EOSEQ != (ADC->ISR & EOSEQ))
			;
		ADC->ISR |= EOSEQ;
		ADC_StopConv();

		if (i == 0)
		{ // Discard the first number
			adcisr = (ADC->uFIFO_DAT.FIFO) & 0xFFFF;
			adcisr = 0;
			continue;
		}
		adcisr += (ADC->uFIFO_DAT.FIFO) & 0xFFFF;
	}

	ADC_Cmd(ADC_DIS);

	return ((adcisr >> 3) & 0x0fff);
}*/

uint8_t TP_IF_PenDown(void)
{
	uint16_t ain0_Value;
	uint8_t value;

	value = OPTION->U16_ADCCDISR.ADCCDIS;
	OPTION->U16_ADCCDISR.ADCCDISTEST = 1;
	OPTION->U16_ADCCDISR.ADCCDISTEST = 2;
	OPTION->U16_ADCCDISR.ADCCDISTEST = 3;
	value |= ((1<<7)|((1<<5)));
	value &=~((1<<6)|((1<<4)));
	OPTION->U16_ADCCDISR.ADCCDIS = value;
//	LTPrintf("OPTION->U16_ADCCDISR.ADCCDIS= x%x  \r\n",OPTION->U16_ADCCDISR.ADCCDIS);

	DelayUS(1);

	RTP_XM_OUT;
	RTP_XM_UP;
	RTP_XM_LEVEL(0);
	RTP_XP_OUT;
	RTP_XP_UP;
	RTP_XP_LEVEL(0);
	RTP_YM_IN;
	RTP_YM_UP;
	RTP_YP_IN;
	RTP_YP_UP;

	DelayUS(5);

	ain0_Value = rtp_Get_ADC_Val(ADC_CH4); // Y+ get ADC value

	DelayUS(1);

//	LTPrintf("AIN0=%d\r\n",ain0_Value);

	if (ain0_Value < 500)
	{
		//printf("down:AIN0=%d\r\n",ain0_Value);
		return 0;
	}
	else
	{
		//printf("up:AIN0=%d\r\n",ain0_Value);
		return 1;
	}
}

uint16_t TP_ReadX_Ver(void)
{
	uint16_t xValue0, tmpX;
	uint8_t value;

	value = OPTION->U16_ADCCDISR.ADCCDIS;
	OPTION->U16_ADCCDISR.ADCCDISTEST = 1;
	OPTION->U16_ADCCDISR.ADCCDISTEST = 2;
	OPTION->U16_ADCCDISR.ADCCDISTEST = 3;
	value |= ((1<<7)|((1<<5)));
	value &=~((1<<6)|((1<<4)));
	OPTION->U16_ADCCDISR.ADCCDIS = value;
	DelayUS(1);

	RTP_XM_OUT;
	RTP_XM_UP;
	RTP_XM_LEVEL(0);
	RTP_XP_OUT;
	RTP_XP_UP;
	RTP_XP_LEVEL(1);
	RTP_YM_IN;
	RTP_YM_UP;
	RTP_YP_IN;
	RTP_YP_UP;

	DelayUS(5);


	xValue0 = rtp_Get_ADC_Val(ADC_CH4); // Y+ get ADC value

	tmpX = xValue0 / 1;
	//	printf("XAD:%d\r\n",tmpX);
	return tmpX;
}

uint16_t TP_ReadY_Ver(void)
{
	uint16_t yValue0, tmpY;
	uint8_t value;

	value = OPTION->U16_ADCCDISR.ADCCDIS;
	OPTION->U16_ADCCDISR.ADCCDISTEST = 1;
	OPTION->U16_ADCCDISR.ADCCDISTEST = 2;
	OPTION->U16_ADCCDISR.ADCCDISTEST = 3;
	value &=~((1<<7)|((1<<5)));
	value |= ((1<<6)|((1<<4)));
	OPTION->U16_ADCCDISR.ADCCDIS = value;
	DelayUS(1);

	RTP_XM_IN;
	RTP_XM_UP;
	RTP_XP_IN;
	RTP_XP_UP;
	RTP_YM_OUT;
	RTP_YM_UP;
	RTP_YM_LEVEL(0);
	RTP_YP_OUT;
	RTP_YP_UP;
	RTP_YP_LEVEL(1);

	DelayUS(5);

	yValue0 = rtp_Get_ADC_Val(ADC_CH5); // X+ get ADC value

	DelayUS(1);

	tmpY = yValue0 / 1;
	//	printf("YAD:%d\r\n",tmpY);

	return tmpY;
}

uint16_t TP_ReadX_Hor(void)
{
	uint16_t xValue0, tmpX;

	RTP_XM_IN;
	RTP_XM_DOWN;
	RTP_XP_IN;
	RTP_XP_DOWN;
	RTP_YM_OUT;
	RTP_YM_UP;
	RTP_YM_LEVEL(0);
	RTP_YP_OUT;
	RTP_YP_UP;
	RTP_YP_LEVEL(1);

	DelayUS(5);

/*	xValue0 = Get_ADC_Val(ADCCH_1); // X+ get ADC value*/

	tmpX = xValue0 / 1;
	//	printf("YAD:%d\r\n",tmpY);

	return tmpX;
}

uint16_t TP_ReadY_Hor(void)
{
	uint16_t yValue0, tmpY;

	RTP_XM_OUT;
	RTP_XM_UP;
	RTP_XM_LEVEL(0);
	RTP_XP_OUT;
	RTP_XP_UP;
	RTP_XP_LEVEL(1);
	RTP_YM_IN;
	RTP_YM_DOWN;
	RTP_YP_IN;
	RTP_YP_DOWN;

	DelayUS(5);

/*	yValue0 = Get_ADC_Val(ADCCH_0); // Y+ get ADC value*/

	tmpY = yValue0 / 1;
//		printf("XAD:%d\r\n",tmpX);
	return tmpY;
}

// Read x, y coordinates
// x,y:read coordinate value
uint8_t TP_Read_XY(uint16_t *x, uint16_t *y)
{
#if LT_TFT_DIR

//		*x=TP_ReadX_Hor();
//		*y=TP_ReadY_Hor();

	*x = TP_ReadX_Ver();
	*y = TP_ReadY_Ver();

#else
	*x = TP_ReadX_Ver();
	*y = TP_ReadY_Ver();
#endif

//	LTPrintf("XAD:%d  ", *x);
//	LTPrintf("YAD:%d \r\n", *y);
	if (*x > 4095 || *y > 4095)
		return 0; // Reading failed
	return 1;
}

/********************************************************************************
* Function Name	: TP_Read_XY2
* Description  	: Read the touch screen IC for two consecutive times, and the deviation between the two times cannot exceed.
* Input        	: - x,y: Read coordinate value
* Output       	: None
* Return       	: 0:error  1:ok
*********************************************************************************/
#define ERR_RANGE 50 // Error range
uint8_t TP_Read_XY2(uint16_t *x, uint16_t *y)
{
	uint16_t x1, y1;
	uint16_t x2, y2;
	uint8_t flag;
	flag = TP_Read_XY(&x1, &y1);
	if (flag == 0)
		return (0);
	flag = TP_Read_XY(&x2, &y2);
	if (flag == 0)
		return (0);
	if (((x2 <= x1 && x1 < x2 + ERR_RANGE) || (x1 <= x2 && x2 < x1 + ERR_RANGE)) // The first and second samples are within+- 50
		&& ((y2 <= y1 && y1 < y2 + ERR_RANGE) || (y1 <= y2 && y2 < y1 + ERR_RANGE)))
	{
		*x = (x1 + x2) / 2;
		*y = (y1 + y2) / 2;
		return 1;
	}
	else
	{
		return 0;
	}
}

// Display correction point
void LT_TpDrawTouchPoint(uint16_t x, uint16_t y, uint32_t color)
{
	LT268_TFT_DrawLine(x - 12, y, x + 13, y, color); // Horizontal line
	LT268_TFT_DrawLine(x, y - 12, x, y + 13, color); // Vertical line

	LT268_TFT_DrawCircle(x, y, 6, color);			 // Draw center circle

}

uint8_t Yp_Pulldown_Flag = 1;

/********************************************************************************
* Function Name	: TP_Read_XY2
* Description  	: touch scan.
* Input        	: None
* Output       	: None
* Return       	: 0:have touch event  1:no touch event
*********************************************************************************/
uint8_t LT_TpScan(void)
{
	Yp_Pulldown_Flag = TP_IF_PenDown();

	if (Yp_Pulldown_Flag == 0 && RTP_read_Flag == 0)
	{
		RTP_read_Flag = 1;
		RTP_time = 0;
//		TP_Read_XY2(&gTpInfo.px,&gTpInfo.py);
		//gTpInfo.x[1]=0;
		//gTpInfo.y[1]=320;
		if (TP_Read_XY2(&gTpInfo.px, &gTpInfo.py))
		{
			gTpInfo.x[1] = gTpInfo.xfac * gTpInfo.px + gTpInfo.xoff;
			gTpInfo.y[1] = gTpInfo.yfac * gTpInfo.py + gTpInfo.yoff;

//		     LTPrintf("gTpInfo.pxy:  %d ,  %d  \r\n",gTpInfo.px,gTpInfo.py);
//			LTPrintf("X: %d   Y:  %d  \r\n",gTpInfo.x[1],gTpInfo.y[1]);

			if (gTpInfo.x[1] > LCD_XSIZE_TFT)
				gTpInfo.x[1] = LCD_XSIZE_TFT;
			if (gTpInfo.y[1] > LCD_YSIZE_TFT)
				gTpInfo.y[1] = LCD_YSIZE_TFT;

			//if(gTpInfo.x[1]==0 &&(gTpInfo.y[1]==320||gTpInfo.y[1]==319))  { return 0; } //Release moment

			if (First_press == 0)
			{
				First_press = 1;
				First_pressX = gTpInfo.x[1];
				First_pressY = gTpInfo.y[1];
			}
			gTpInfo.x[0] = gTpInfo.x[1];
			gTpInfo.y[0] = gTpInfo.y[1];
//			LTPrintf("X: %d   Y:  %d  \r\n",gTpInfo.x[0],gTpInfo.y[0]);

			if (gTpInfo.sta == 0)gTpInfo.sta = 1;
	//		printf("press    \r\n");
		}
		return 1;
	}
	else if (RTP_read_Flag)
	{
		return 0;
	}
	else
	{
//				printf("lift ------   \r\n");
		First_press = 0;
		RTP_read_Flag = 0;
		gTpInfo.sta = 0;

		return 0;
	}
}

uint8_t LT_TpScan_cal(void)
{
	Yp_Pulldown_Flag = TP_IF_PenDown();

	if (Yp_Pulldown_Flag == 0)
	{
//		RTP_read_Flag = 1;
		RTP_time =0;
//		TP_Read_XY2(&gTpInfo.px,&gTpInfo.py);

		if (TP_Read_XY2(&gTpInfo.px, &gTpInfo.py))
		{
			gTpInfo.x[1] = gTpInfo.xfac * gTpInfo.px + gTpInfo.xoff;
			gTpInfo.y[1] = gTpInfo.yfac * gTpInfo.py + gTpInfo.yoff;
//			LTPrintf("gTpInfo.pxy:  %d ,  %d  \r\n",gTpInfo.px,gTpInfo.py);
			if (gTpInfo.sta == 0)  gTpInfo.sta = 1;

		}
		return 1;
	}
	else if (RTP_read_Flag)
	{
		return 0;
	}
	else
	{
		First_press = 0;
		RTP_read_Flag = 0;
		gTpInfo.sta = 0;

		return 0;
	}
}
// Prompt string
uint8_t *const TP_REMIND_MSG_TBL = (uint8_t *)"Please correct the resistance screen";

// Touch screen calibration
// Get four calibration parameters
uint8_t LT_TpAdjust(void)
{
	uint8_t cnt = 0;
	uint16_t outTime = 0;
	uint16_t d1, d2;
	uint32_t temp1, temp2;
	uint16_t posTemp[2][4];
	float fac;
	uint8_t flag = 0;
	uint8_t flag2 = 0;
	uint8_t show = 0;
	LT268_TFT_DrawSquare_Fill(0, 0, LCD_XSIZE_TFT - 1, LCD_YSIZE_TFT - 1, White);
	LT268_TFT_ShowAscll(40, 40, 24, 0, Red, White, (uint8_t *)"Please correct the resistance screen");

	LT_TpDrawTouchPoint(20, 20, Red);
	DelayMS(100);

	while (1)
	{
		WDT_FeedDog();
		if (flag2 == 1)
		{
			LT_TpDrawTouchPoint(LCD_XSIZE_TFT - 20, LCD_YSIZE_TFT - 20, White);
			LT_TpDrawTouchPoint(20, 20, Red);
			LT268_TFT_ShowAscll(40, 40, 24, 0, Red, White, (uint8_t *)"Check Failure,Please recheck!");
		}
		cnt = 0;
		flag = 0;
		gTpInfo.sta = 0;

		while (1) // 1
		{
			WDT_FeedDog();
			LT_TpScan_cal();
			DelayMS(50);

			if (gTpInfo.sta)
			{
				posTemp[0][cnt] = gTpInfo.px;
				posTemp[1][cnt] = gTpInfo.py;
				flag = 1;
				if (show == 0)
				{
					show++;
					LT268_TFT_DrawSquare_Fill(0, 0, LCD_XSIZE_TFT, LCD_YSIZE_TFT, White);
					LT_TpDrawTouchPoint(LCD_XSIZE_TFT - 20, 20, Red); // 2
				}
			}
			if (gTpInfo.sta == 0 && flag == 1)
				break;
		}
		DelayMS(300);
		WDT_FeedDog();
		cnt = 1;
		flag = 0;
		gTpInfo.sta = 0;
		while (1) // 2
		{
			WDT_FeedDog();
			LT_TpScan_cal();
			DelayMS(50);


			if (gTpInfo.sta)
			{

				posTemp[0][cnt] = gTpInfo.px;
				posTemp[1][cnt] = gTpInfo.py;
				flag = 1;
				if (show == 1)
				{
					show++;
					LT268_TFT_DrawSquare_Fill(0, 0, LCD_XSIZE_TFT, LCD_YSIZE_TFT, White);
					LT_TpDrawTouchPoint(20, LCD_YSIZE_TFT - 20, Red); // 3
				}
			}
			if (gTpInfo.sta == 0 && flag == 1)
				break;
		}
		DelayMS(300);
		WDT_FeedDog();
		cnt = 2;
		flag = 0;
		gTpInfo.sta = 0;
		while (1) // 3
		{
			WDT_FeedDog();
			LT_TpScan_cal();
			DelayMS(50);


			if (gTpInfo.sta)
			{
				posTemp[0][cnt] = gTpInfo.px;
				posTemp[1][cnt] = gTpInfo.py;
				flag = 1;
				if (show == 2)
				{
					show = 0;
					LT268_TFT_DrawSquare_Fill(0, 0, LCD_XSIZE_TFT, LCD_YSIZE_TFT, White);
					LT_TpDrawTouchPoint(LCD_XSIZE_TFT - 20, LCD_YSIZE_TFT - 20, Red); // 4
				}
			}
			if (gTpInfo.sta == 0 && flag == 1)
				break;
		}
		DelayMS(300);
		WDT_FeedDog();
		cnt = 3;
		flag = 0;
		gTpInfo.sta = 0;
		while (1) // 4
		{
			WDT_FeedDog();
			LT_TpScan_cal();
			DelayMS(50);


			if (gTpInfo.sta)
			{
				gTpInfo.sta = 0;
				posTemp[0][cnt] = gTpInfo.px;
				posTemp[1][cnt] = gTpInfo.py;
				flag = 1;
			}
			if (gTpInfo.sta == 0 && flag == 1)
				break;
		}

		temp1 = abs(posTemp[0][0] - posTemp[0][1]);
		temp2 = abs(posTemp[1][0] - posTemp[1][1]);
		temp1 *= temp1;
		temp2 *= temp2;
		d1 = sqrt(temp1 + temp2);

		temp1 = abs(posTemp[0][2] - posTemp[0][3]);
		temp2 = abs(posTemp[1][2] - posTemp[1][3]);
		temp1 *= temp1;
		temp2 *= temp2;
		d2 = sqrt(temp1 + temp2);
		fac = (float)d1 / d2;

		if (fac < 0.85f || fac > 1.15f || d1 == 0 || d2 == 0)
		{
			flag2 = 1;
			LT_TpDrawTouchPoint(LCD_XSIZE_TFT - 20, LCD_YSIZE_TFT - 20, White);
			LT_TpDrawTouchPoint(20, 20, Red);
			LT268_TFT_ShowAscll(40, 40, 24, 0, Red, White, (uint8_t *)"Check Failure,Please recheck!");
			DelayMS(1000);
			WDT_FeedDog();
			continue;
		}
		// Calculate the ratio of the left and right sides
		temp1 = abs(posTemp[0][0] - posTemp[0][2]);
		temp2 = abs(posTemp[1][0] - posTemp[1][2]);
		temp1 *= temp1;
		temp2 *= temp2;
		d1 = sqrt(temp1 + temp2);

		temp1 = abs(posTemp[0][1] - posTemp[0][3]);
		temp2 = abs(posTemp[1][1] - posTemp[1][3]);
		temp1 *= temp1;
		temp2 *= temp2;
		d2 = sqrt(temp1 + temp2);

		if (fac < 0.85f || fac > 1.15f || d1 == 0 || d2 == 0)
		{
			flag2 = 1;
			LT_TpDrawTouchPoint(LCD_XSIZE_TFT - 20, LCD_YSIZE_TFT - 20, White);
			LT_TpDrawTouchPoint(20, 20, Red);
			LT268_TFT_ShowAscll(40, 40, 24, 0, Red, White, (uint8_t *)"Check Failure,Please recheck!");
			DelayMS(1000);
			WDT_FeedDog();
			continue;
		}

		// Calculate the proportion of the length of the opposite side
		temp1 = abs(posTemp[0][0] - posTemp[0][3]);
		temp2 = abs(posTemp[1][0] - posTemp[1][3]);
		temp1 *= temp1;
		temp2 *= temp2;
		d1 = sqrt(temp1 + temp2);

		temp1 = abs(posTemp[0][1] - posTemp[0][2]);
		temp2 = abs(posTemp[1][1] - posTemp[1][2]);
		temp1 *= temp1;
		temp2 *= temp2;
		d2 = sqrt(temp1 + temp2);

		fac = (float)d1 / d2;

		if (fac < 0.85f || fac > 1.15f || d1 == 0 || d2 == 0)
		{
			flag2 = 1;
			LT_TpDrawTouchPoint(LCD_XSIZE_TFT - 20, LCD_YSIZE_TFT - 20, White);
			LT_TpDrawTouchPoint(20, 20, Red);
			LT268_TFT_ShowAscll(40, 40, 24, 0, Red, White, (uint8_t *)"Check Failure,Please recheck!");
			flag2 = 1;
			DelayMS(1000);
			WDT_FeedDog();
			continue;
		}

		/* If the ratio of the upper and lower sides, the ratio of the left and right sides, 
		   and the ratio of the diagonal side are all within the range, the four correction points meet the requirements*/
		gTpInfo.xfac = (float)(LCD_XSIZE_TFT - 40) / (posTemp[0][1] - posTemp[0][0]);
		gTpInfo.xoff = (LCD_XSIZE_TFT - gTpInfo.xfac * (posTemp[0][1] + posTemp[0][0])) / 2;
		gTpInfo.yfac = (float)(LCD_YSIZE_TFT - 40) / (posTemp[1][2] - posTemp[1][0]);
		gTpInfo.yoff = (LCD_YSIZE_TFT - gTpInfo.yfac * (posTemp[1][2] + posTemp[1][0])) / 2;

		gTpxfac.xfac = gTpInfo.xfac;
		gTpxoff.xoff = gTpInfo.xoff;
		gTpyfac.yfac = gTpInfo.yfac;
		gTpyoff.yoff = gTpInfo.yoff;

		LT268_TFT_DrawSquare_Fill(0, 0, LCD_XSIZE_TFT, LCD_YSIZE_TFT, White);
		LT268_TFT_ShowAscll(40, 40, 24, 0, Red, White, (uint8_t *)"Touch Screen Adjust OK!"); 
		DelayMS(1000);
		WDT_FeedDog();
		// Save 4 parameters
		LT_TpSaveAdjdata();
		return 1;
	}
}

uint8_t LT_TpInit(void)
{
	ADC_Initial();
	DelayMS(10);
	if (LT_TpGetAdjdata() != 1)
	{
		DelayMS(100);
		LCD_BL_Init(249); // Backlight Setting 0~499
		LT_TpAdjust();
	}
	TP_Read_XY2(&gTpInfo.px, &gTpInfo.py);

	gTpInfo.scan = LT_TpScan;

	return 1;
}

#endif
